/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package otros;

import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.awt.font.TextAttribute;
import java.awt.print.*;
import java.awt.print.PrinterJob;
import java.awt.print.PageFormat;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.AttributedString;
import java.text.DecimalFormat;
import javax.print.DocPrintJob;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.attribute.AttributeSet;
import javax.print.attribute.HashAttributeSet;
import javax.print.attribute.standard.PrinterName;
import javax.swing.JTable;
import tablasSql.ConsultaBD;

/**
 *
 * @author FERRETERIA
 */


class ObjetoDeImpInforme extends Component implements Printable{
    
    private DatosToImprimir d;
    int posY;
    int NUM_LINEAS;
    
    public ObjetoDeImpInforme (DatosToImprimir d, int tamLineas) {
        super();
        NUM_LINEAS = tamLineas;
        this.d = d;
    } 
    
    
    private int getIniPosX() { 
       return 56;
    }
        
    public void EncabezadoA4(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
         
        int y = 10;
        posY = 80;
        int posX = getIniPosX();
        
        g.setFont(new Font ("Calibri", Font.BOLD, 16));
        g.drawString(d.getFamilia(), posX, posY);      // x,y
        g.setFont(new Font ("Calibri", Font.PLAIN, 12));
        g.drawString("Pag.: "+(d.getPagina()+1), 450,posY);
        g.drawString("Nº Total Art.: "+d.getNumCopias(), posX,posY+=13);
  
        g.drawLine(posX, posY +=5, 540, posY);
    }
    
    
    
    private int getDespazamiento(int i) {
        return (i==1) ? 215 : 50;
    }
    
    public void Datos(Graphics g) {
        int posX = getIniPosX();
        int y;
        
        JTable tabla = d.getTabla();
        
        posY += 13;
        int x = 0;
        g.setFont(new Font ("Calibri", Font.BOLD, 12));
        for (int i = 0; i < tabla.getColumnCount(); i++) {
            g.drawString(tabla.getColumnName(i), posX + x, posY);
            x += getDespazamiento(i);
        }
        
        y = 14;
        posY += y;
        g.setFont(new Font ("Calibri", Font.PLAIN, 10));
        
        int j = 0;
        for (int i = (d.getPagina()* NUM_LINEAS); (i < tabla.getRowCount()) && (i < (d.getPagina()+1) * NUM_LINEAS); i++) {
            x = 0;
            for (int k = 0; k < tabla.getColumnCount(); k++) {
                String valor = String.valueOf(tabla.getValueAt(i, k));
                if (tabla.getColumnName(k).equals("Costo")) valor = Util.getFormatPrecio(Float.parseFloat(valor), 3);
                if (tabla.getColumnName(k).equals("PVP")) valor = Util.getFormatPrecio(Float.parseFloat(valor), 2);
                g.drawString(valor, posX + x, posY + y * j);
                x += getDespazamiento(k);
            }
            j++;
        } 
        posY += y*j;
    }
    
    public void Pie(Graphics g) {
         
    }
               
    public int print(Graphics g, PageFormat f, int pageIndex){
        switch (pageIndex) {
            case 0:
               EncabezadoA4(g);
               Datos(g);
        //       Pie(g);
               return PAGE_EXISTS;
            default:
                return NO_SUCH_PAGE;
        }
    }
}

public class ImpInforme {
    
    static final int NUM_LINEAS = 49;
    static String impresora;
    
    void Imprimir () {
        setImpresora();
    }
    
    static void setImpresora () {
        impresora = Configuracion.getInstance().getProperty(Configuracion.IMP_NORMAL);
    }
    
    static public void imprime(DatosToImprimir d){
        ConsultaBD.cursorEspera(null);
        setImpresora();
        PrinterJob job = PrinterJob.getPrinterJob();
        PageFormat pf = job.defaultPage();
        Paper paper = new Paper();
        double margin;
        paper.setSize(595.0,842.0); // ancho y alto  A4
        margin = 10;
        
        
        paper.setImageableArea(margin, margin, paper.getWidth()-margin, paper.getHeight()-margin);
        pf.setPaper(paper);
        pf.setOrientation(PageFormat.PORTRAIT);  // vertical
        job.setJobName("impreso");
        
        PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
        for (PrintService printService : services) {
            if (printService.getName().equals(impresora)) {
                try {
                    job.setPrintService(printService);
                    break;
                } catch (PrinterException ex) {
                    System.out.println(ex);
                }
            }
        }
        try{
           for (int j = 0; j <=(d.getTabla().getRowCount()/50); j++) {
               d.setPagina(j);
               job.setPrintable(new ObjetoDeImpInforme(d, NUM_LINEAS), pf);  
               job.print();
           }
        } catch(PrinterException e){
             System.out.println(e);
        }

        ConsultaBD.cursorNormal(null);
    }
}
